package org.eclipse.emf.db.sqlite;

import static java.text.MessageFormat.format;

import java.sql.Connection;
import java.sql.SQLException;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.extensions.DBSchema;
import org.eclipse.emf.extensions.IDBTable;
import org.eclipse.emf.extensions.IDBTableDescriptor;
import org.eclipse.emf.extensions.IDBTypeProvider;
import org.eclipse.emf.extensions.mysql.MySQLTypeProvider;
import org.sqlite.SQLiteJDBCLoader;

public class SQLiteSchema extends DBSchema {
    public static final String[] RESERVED_WORDS=new String[] { "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ATTACH",
            "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", "CONFLICT", "CONSTRAINT",
            "CREATE", "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DESC",
            "DETACH", "DISTINCT", "DROP", "EACH", "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", "FAIL", "FOR", "FOREIGN", "FROM",
            "FULL", "GLOB", "GROUP", "HAVING", "IF", "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT", "INSTEAD", "INTERSECT",
            "INTO", "IS", "ISNULL", "JOIN", "KEY", "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", "NULL", "OF", "OFFSET", "ON", "OR",
            "ORDER", "OUTER", "PLAN", "PRAGMA", "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", "RENAME", "REPLACE",
            "RESTRICT", "RIGHT", "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER",
            "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", "WITH", "WITHOUT" };
    public SQLiteSchema(Connection connection, EPackage pkg) throws SQLException {
        super(connection, pkg);
    }

    @Override
    public boolean requireIndexLength() {
        return false;
    }

    @Override
    protected IDBTable createTable(DBSchema schema, IDBTableDescriptor descriptor) {
        return new SQLiteTable(schema, descriptor);
    }

    @Override
    protected String getCreateTableSQL(IDBTableDescriptor descriptor) {
        return format("CREATE TABLE {0} (cdo_id BIGINT PRIMARY KEY,cdo_created BIGINT NON NULL, cdo_container BIGINT)", descriptor.getName()); //$NON-NLS-1$
    }

    @Override
    protected String[] getReservedWords() {
        return RESERVED_WORDS;
    }

    @Override
    public String getDBVersion() {
        return SQLiteJDBCLoader.getVersion();
    }

    @Override
    public IDBTypeProvider getTypeProvider() {
        return MySQLTypeProvider.INSTANCE;
    }

    @Override
    public boolean checkModificationTime() {
        return false;
    }
}
